/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __XPINN__CALLBACKS_H__
#define __XPINN__CALLBACKS_H__

#include "xpinn.h"
#include <XPLMProcessing.h>
#include <XPLMDisplay.h>
#include <functional>

/**
 * Manage a flight loop callback. Supports the C++ RAII idiom.
 */
class XPInnFlightLoopCallback
{
public:
  /**
   * The type of function object which the callback's owner provides.
   *
   * The function should return true to indicate that it should remain
   * enabled, false if it should be disabled.
   */
  typedef std::tr1::function<bool(float, float)> function_type;
  
  /**
   * Constructor.
   *
   * @param interval Time between calls. Positive numbers are seconds,
   * negative numbers are frame counts.
   * @param func The callback function object.
   * @param enable Whether the callback will initially be enabled. Default is false.
   */
  XPInnFlightLoopCallback(float interval, function_type const& func, bool enable = false);

  /**
   * Destructor.
   */
  ~XPInnFlightLoopCallback();

  /**
   * Enable the callback. The callback will be called during the next frame,
   * and thereafter will be called after every `interval`.
   */
  void enable();

  /**
   * Disable the callback. The callback will not be called again until the
   * next call to `enable`.
   *
   * @warning Do not call this function from within your callback function.
   * Use a value of `false` for the callback function's return value instead.
   */
  void disable();

private:
  float m_interval;
  function_type m_func;

  static float wrapperFunction(float myElapsed, float loopElapsed, int count, void* refcon);
};

/**
 * Manage a drawing callback. Supports the C++ RAII idiom.
 */
class XPInnDrawCallback
{
public:
  /**
   * The type of function object which the callback's owner provides.
   */
  typedef std::tr1::function<void()> function_type;

  /**
   * Constructor. Drawing callbacks do not have a notion of being enabled or
   * disabled; they are "always on".
   *
   * @param phase Phase in the drawing loop during which we want to be called.
   * @param before Whether we want to be called before that phase or afterwards.
   * @param func The callback function object.
   */
  XPInnDrawCallback(XPLMDrawingPhase phase, bool before, function_type const& func);

  /**
   * Destructor.
   */
  ~XPInnDrawCallback();

private:
  XPLMDrawingPhase m_phase;
  bool m_before;
  function_type m_func;

  static int wrapperFunction(XPLMDrawingPhase phase, int before, void* refcon);
};

#endif //__XPINN__CALLBACKS_H__